package com.shuai.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shuai.stickynote.R;
import com.shuai.stickynote.TaskNote;

import java.util.ArrayList;

public class TaskListChildAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<TaskNote> tasks;

    public TaskListChildAdapter(Context context) {
        this.context = context;
    }

    public void replaceAll(ArrayList<TaskNote> data){
        if (tasks == null) tasks = new ArrayList<>();
        tasks.clear();
        if (data != null && data.size() > 5){
            for (int i = 0; i < 5; i++) {
                tasks.add(data.get(i));
            }
        }else if (data != null){
            tasks.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tasks.size();
    }
    @Override
    public Object getItem(int position) {
        return tasks.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_task_list_chird,null);
            holder = new ViewHolder();
            holder.tv_countTag = convertView.findViewById(R.id.item_task_list_chird_num);
            holder.tv_content = convertView.findViewById(R.id.item_task_list_chird_content);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position < 5){
            if (tasks.get(position).getTagType().equals(TaskNote.TYPE_SPOT)){
                holder.tv_countTag.setText("●");
                holder.tv_countTag.setTextSize(8);
            } else {
                holder.tv_countTag.setText((position+1)+".");
                holder.tv_countTag.setTextSize(20);
            }
            holder.tv_content.setText(tasks.get(position).getContent());
            if (tasks.get(position).getComplete() == 1){
                holder.tv_content.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }else{
                holder.tv_content.getPaint().setFlags(0);
            }
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_countTag,tv_content;
    }

}
